package com.bascker.beauty.ifelse;

import com.google.common.collect.Lists;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 工厂模式 + 策略模式 + toMap： 消除 if-else
 */
public class StrategyFactory {

    // key: brand
    private final Map<String, CarStrategy> map;

    private StrategyFactory() {
        final List<CarStrategy> strategies = Lists.newArrayList();
        strategies.add(new BMWStrategy());
        strategies.add(new CadillacStrategy());

        map = strategies.stream().collect(Collectors.toMap(CarStrategy::brand, strategy -> strategy));
    }

    private static class Holder {
        public static StrategyFactory instance = new StrategyFactory();
    }

    public static StrategyFactory getInstance() {
        return Holder.instance;
    }

    public CarStrategy get(String brand) {
        return map.get(brand);
    }

}
